package com.yilin.tms.order.controller;

import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.commons.model.CalculateCost;

import com.yilin.tms.order.service.payment.ICalculationService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/order/calculation/")
public class CalculationController extends BaseController {

    @Resource
    ICalculationService calculationService;

    /**
     * 計算會員費用
     * 可以只计算手续费，税费
     */
    @RequestMapping(value = "getShipperCalculateCost", method = RequestMethod.POST)
    @NotNull({"priceMode", "matching", "includedTax"})
    public ReturnData<CalculateCost> getShipperCalculateCost(@Explain("货主组织ID") String shipperOrganizeId, @Explain("计价模式") GoodsOrder.PriceMode priceMode,
                                                             @Explain("是否撮合") Boolean matching, @Explain("是否含税") Boolean includedTax,
                                                             Double shipperTransportCost, String consignorCoordinates, String consigneeCoordinates, String truckTypeIds) {
        if (shipperOrganizeId == null) shipperOrganizeId = getLoginUser().getOrganizeId();
        CalculateCost calculateCost = calculationService.getShipperCalculateCost(shipperOrganizeId, priceMode, shipperTransportCost, consignorCoordinates, consigneeCoordinates, truckTypeIds, matching, includedTax);
        return jsonView(true, "自动计算费用成功", calculateCost);
    }

    /**
     * 计算车主会员费用
     */
    @PostMapping(value = "getTruckerCalculateCost")
    @NotNull({"freight", "matching"})
    @UserTarget({UserType.truckerUser, UserType.shipperUser})
    public ReturnData<CalculateCost> getTruckerCalculateCost(@Explain("车主组织ID") String truckerOrganizeId, @Explain("运输费") Double freight,
                                                             @Explain("是否撮合") Boolean matching) {
        if (truckerOrganizeId == null) truckerOrganizeId = getLoginUser().getOrganizeId();
        CalculateCost calculateCost = calculationService.getTruckerCalculateCost(truckerOrganizeId, freight, matching);
        return jsonView(true, "获取成功", calculateCost);
    }

    /**
     * 计算技术费用
     */
    @PostMapping(value = "calculateTechnicalFee")
    public ReturnData<Double> calculateTechnicalFee(@Explain("服务费") Double technicalFee) {
        Double calculateCost = calculationService.calculateTechnicalFee(technicalFee);
        return jsonView(true, "获取成功", calculateCost);
    }

    /**
     * 计技术算费用 平台
     */
    @PostMapping(value = "calculatePlatTechnicalFee")
    public ReturnData<Double> calculatePlatTechnicalFee(@Explain("平台ID") String platId,@Explain("服务费") Double technicalFee) {
        Double calculateCost = calculationService.calculatePlatTechnicalFee(platId,technicalFee);
        return jsonView(true, "获取成功", calculateCost);
    }
}
